// 导入依赖
import { jsonp } from 'vue-jsonp'
import {  ref ,onMounted,toRefs } from 'vue';
// 导入pinia仓库
import { storeToRefs } from "pinia";
import {CStore} from '../../stores/counter'
const $store = CStore()
const {currentCityDate,locationCity} = toRefs($store)

// 将解析地名的代码封装
export const analysisCityName = async ( name)=> {
    const key = 'OUTBZ-UTV3X-VMD4C-ZGR7M-5OG35-OPBG6'

    // 发送请求进行地址解析
    let res2 = await jsonp(`https://apis.map.qq.com/ws/geocoder/v1/?address=${name}&key=${key}`,{
        output:'jsonp',   
    })

    // 获取并存储位置信息
   const location = [res2.result.location.lng,res2.result.location.lat]
    
   let adcode = ''
    // 获取并存储位置adcode
    if(name == '北京'||name == '天津'||name == '上海'||name == '重庆'){
 
       adcode = res2.result.ad_info.adcode.slice(0,3) + '000'
    }else {

       adcode = res2.result.ad_info.adcode.slice(0,4) + '00'
    }

    $store.changeCurrentCity(name,location,adcode)
}